package com.olddriver.crm.service.impl;

import com.olddriver.crm.domain.Carproduct;
import com.olddriver.crm.domain.Employee;
import com.olddriver.crm.domain.InsuranceList;
import com.olddriver.crm.mapper.CarinformationMapper;
import com.olddriver.crm.mapper.ClientMapper;
import com.olddriver.crm.mapper.InsuranceListMapper;
import com.olddriver.crm.mapper.MechanismMapper;
import com.olddriver.crm.page.PageResult;
import com.olddriver.crm.query.QueryObject;
import com.olddriver.crm.service.IInsuranceListService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class InsuranceListServiceImpl implements IInsuranceListService {


    @Autowired
    private InsuranceListMapper insuranceMapper;

    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    private CarinformationMapper carinformationMapper;

    @Autowired
    private MechanismMapper mechanismMapper;

    public int deleteByPrimaryKey(Long id) {
        return insuranceMapper.deleteByPrimaryKey(id);
    }

    public int insert(InsuranceList record,Long[] ids) {

        if (record.getClient().getId() == null) {
            record.getClient().setStatus(new Byte("0"));
            record.getClient().setInputUser((Employee) SecurityUtils.getSubject().getPrincipal());
            record.getClient().setInputTime(new Date());
            clientMapper.save(record.getClient());
        }

        if (record.getCarInformation().getId() == null) {
            carinformationMapper.insert(record.getCarInformation());
        }

        if (record.getProductItem().getId()==null){
            mechanismMapper.insert(record.getProductItem());
        }
        record.setState(new Byte("0"));
        record.setSn(((Long) System.currentTimeMillis()).toString());
        record.setInputtime(new Date());
        record.setVerifytime(new Date());
        record.setVerifyUser((Employee) SecurityUtils.getSubject().getPrincipal());
        int row = insuranceMapper.insert(record);
        insuranceMapper.insertRelation(record.getId(),ids);
        return row;
    }

    public InsuranceList selectByPrimaryKey(Long id) {
        return insuranceMapper.selectByPrimaryKey(id);
    }

    public List<InsuranceList> selectAll() {
        return insuranceMapper.selectAll();
    }

    public int updateByPrimaryKey(InsuranceList record,Long[] ids) {

        //删除关系
        insuranceMapper.deleteRelation(record.getId());

        //建立关系
        insuranceMapper.insertRelation(record.getId(),ids);

        clientMapper.update(record.getClient());
        carinformationMapper.updateByPrimaryKey(record.getCarInformation());
        mechanismMapper.updateByPrimaryKey(record.getProductItem());

        return insuranceMapper.updateByPrimaryKey(record);

    }

    public PageResult query(QueryObject qo) {
        int total = insuranceMapper.queryTotal(qo);
        if (total == 0) {
            return new PageResult(total, Collections.emptyList());
        }
        return new PageResult(total, insuranceMapper.queryRows(qo));
    }

    @Override
    public void refuse(Long id) {
        insuranceMapper.refuse(id);
    }

    @Override
    public void prepareAudit(Long id) {
        insuranceMapper.prepareAudit(id);
    }

    @Override
    public void passAudit(Long id) {
        insuranceMapper.passAudit(id);
    }

    @Override
    public void sendBack(Long id) {
        insuranceMapper.sendBack(id);
    }

    @Override
    public List<Carproduct> getCarproductByInsuId(Long id) {
        return insuranceMapper.getCarproductByInsuId(id);
    }

    @Override
    public int getUndealOrder() {
        return insuranceMapper.getUndealOrder();
    }

    @Override
    public int getUnpaidOrder() {
        return insuranceMapper.getUnpaidOrder();
    }

}
